package zmee.pur.src.opplugin;

import com.alibaba.excel.util.ObjectUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.List;

/**
 * @Author: xw
 * @Date: 2023/02/20/15:04
 * @Description: 根据标书编制是否发送公告字段选择公告范围字段
 */
public class BidDocAuditOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("zmee_ispaper");
        fieldKeys.add("billno");
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntity : dataEntities) {
            boolean ispaper = dataEntity.getBoolean("zmee_ispaper");
            if (!ispaper) {
                Object srcBillNo = dataEntity.get("billno");
                QFilter filter = new QFilter("srcbillno", "=", srcBillNo);
                DynamicObject souNotice = BusinessDataServiceHelper.loadSingle("sou_notice", "supscope", filter.toArray());
                if (ObjectUtils.isEmpty(souNotice)) {
                    return;
                }
                souNotice.set("supscope", "3");
                SaveServiceHelper.save(new DynamicObject[]{souNotice});

                QFilter filter1 = new QFilter("billno", QCP.equals, srcBillNo);
                DynamicObject publishNotice = BusinessDataServiceHelper.loadSingle("src_publishnotice", "zmee_noticway", filter1.toArray());
                if (ObjectUtils.isEmpty(publishNotice)) {
                    return;
                }
                publishNotice.set("zmee_noticway", "4");
                SaveServiceHelper.save(new DynamicObject[]{publishNotice});
            }
        }
    }
}
